/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				TrainBOPlan
 
Function						: 				Provides a Plan for training 
												a particular unit
****************************************************************************/
package planninglanguage.plans;

import org.jdom.Element;

import planninglanguage.conditions.AndCondition;
import planninglanguage.conditions.Condition;
import planninglanguage.conditions.GoldCondition;
import planninglanguage.conditions.OilCondition;
import planninglanguage.conditions.SequentialCondition;
import planninglanguage.conditions.StatusCondition;
import planninglanguage.conditions.SupplyCondition;
import planninglanguage.conditions.UnitExistsCondition;
import planninglanguage.conditions.WoodCondition;
import util.XMLWriter;
import base.ProxyBot;
import base.WargusStateImporter;

public class TrainBOPlan extends BasicOperatorPlan implements Cloneable{

	int m_unitType;
	int m_constructedUnitID;	// This stores which was the ID of the unit trained in the expert's trace
	
	public TrainBOPlan(int a_unitID,int a_unitType,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition)
	{
		super(a_precondition,a_aliveCondition,a_successCondition);
		m_unitID=a_unitID;
		m_unitType=a_unitType;
		
		m_precondition = new AndCondition(new SupplyCondition(1), 
							new AndCondition(new StatusCondition(m_unitID,"idle"),
										  new AndCondition(new GoldCondition(WargusStateImporter.unitCostGold(WargusStateImporter.unitTypeToString(m_unitType))),
												           new AndCondition(new WoodCondition(WargusStateImporter.unitCostWood(WargusStateImporter.unitTypeToString(m_unitType))),
												        		   			new OilCondition(WargusStateImporter.unitCostOil(WargusStateImporter.unitTypeToString(m_unitType)))))));
		m_successCondition = new SequentialCondition(new StatusCondition(m_unitID,"working"),
				 			 						 new StatusCondition(m_unitID,"idle"));
		m_aliveCondition = new UnitExistsCondition(m_unitID);		
	}
	
	public TrainBOPlan(int a_unitID,int a_unitType,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition, Plan a_parent)
	{
		super(a_precondition,a_aliveCondition,a_successCondition);
		m_unitID=a_unitID;
		m_unitType=a_unitType;
		
		m_precondition = new AndCondition(new SupplyCondition(1), 
							new AndCondition(new StatusCondition(m_unitID,"idle"),
										  new AndCondition(new GoldCondition(WargusStateImporter.unitCostGold(WargusStateImporter.unitTypeToString(m_unitType))),
												           new AndCondition(new WoodCondition(WargusStateImporter.unitCostWood(WargusStateImporter.unitTypeToString(m_unitType))),
												        		   			new OilCondition(WargusStateImporter.unitCostOil(WargusStateImporter.unitTypeToString(m_unitType)))))));
		m_successCondition = new SequentialCondition(new StatusCondition(m_unitID,"working"),
				 			 						 new StatusCondition(m_unitID,"idle"));
		m_aliveCondition = new UnitExistsCondition(m_unitID);		
		m_parent = a_parent;
	}
	
	public Object clone()
	{
		TrainBOPlan l_p =new TrainBOPlan(m_unitID, m_unitType, null, null, null, m_parent);
		l_p.setContext(m_context);
		l_p.m_originalUnit = m_originalUnit;
		return l_p;
	}
	
	public int getUnitType(){
		return m_unitType;
	}
	
	public void setConstructedUnitID(int a_constructedUnitID) {
		m_constructedUnitID = a_constructedUnitID;
	}
	
	public int getConstructedUnitID() {
		return m_constructedUnitID;
	}	

	public boolean send()
	{
		ProxyBot.getProxyBot().train(m_unitID,m_unitType);
		return true;
	}
	
	public String toString(int tabs) {
		String str= "";
		int i;
		
		for(i=0;i<tabs;i++) str = str + "  ";
		str = str + "TrainBOPlan(status = " + STATUS_NAME[m_status] + " , unitID = " + m_unitID + " unitType = " + m_unitType + " ) ";
		str = str + conditionsToString(tabs);
		return str;
	}		
	
	public boolean sameGoal(Object other)
	{
		if (getClass() != other.getClass()) return false;

		TrainBOPlan o = (TrainBOPlan) other;
		   
		if (m_unitID!=o.m_unitID) return false;	
		if (m_unitType!=o.m_unitType) return false;
		return true;
	}		
	
	public void writeXML(XMLWriter w) {
		w.tag(super.XMLopenTag() + " type=\"" + 
				this.getClass().getSimpleName() + "\"");
		{
			w.tag(Plan.XML_VALUE, WargusStateImporter.unitTypeToString(m_unitType));
			w.tag("constructed-unit-id", m_constructedUnitID);
			
			super.writeBOPlanAttrsXML(w);
		}
		w.tag(super.XMLcloseTag());
	}
	
	protected static TrainBOPlan loadFromXML(Element e) {
		int type = WargusStateImporter.unitTypeToInteger(e.getChildText(Plan.XML_VALUE));
		
		TrainBOPlan plan = new TrainBOPlan(-1, type, null, null, null);
		plan.setConstructedUnitID(Integer.parseInt(e.getChildText("constructed-unit-id")));	
		return plan;
	}
}
